package com.yangxk.kadmin.web.common;

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 限流服务
 * @author yangxk
 * @date 2019/3/3 14:20
 */
@Component
public class AccessLimitService {

    private RateLimiter rateLimiter;

    @Value("${access.limit}")
    private Double limit = 5.0;

    @PostConstruct
    public void init() {
        rateLimiter = RateLimiter.create(limit);
    }

    /**
     * 获取令牌
     * create by yangxk 2019/3/3 14:18
     */
    public boolean tryAcquire() {
        return rateLimiter.tryAcquire();
    }
}
